home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / js / jslong.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  15KB  |  438 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Mozilla Communicator client code, released
  16.  * March 31, 1998.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications Corporation.
  20.  * Portions created by the Initial Developer are Copyright (C) 1998
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  27.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /*
  40. ** File:                jslong.h
  41. ** Description: Portable access to 64 bit numerics
  42. **
  43. ** Long-long (64-bit signed integer type) support. Some C compilers
  44. ** don't support 64 bit integers yet, so we use these macros to
  45. ** support both machines that do and don't.
  46. **/
  47. #ifndef jslong_h___
  48. #define jslong_h___
  49.  
  50. #include "jstypes.h"
  51.  
  52. JS_BEGIN_EXTERN_C
  53.  
  54. /***********************************************************************
  55. ** DEFINES:     JSLL_MaxInt
  56. **              JSLL_MinInt
  57. **              JSLL_Zero
  58. ** DESCRIPTION:
  59. **      Various interesting constants and static variable
  60. **      initializer
  61. ***********************************************************************/
  62. #ifdef HAVE_WATCOM_BUG_2
  63. JSInt64 __pascal __loadds __export
  64.     JSLL_MaxInt(void);
  65. JSInt64 __pascal __loadds __export
  66.     JSLL_MinInt(void);
  67. JSInt64 __pascal __loadds __export
  68.     JSLL_Zero(void);
  69. #else
  70. extern JS_PUBLIC_API(JSInt64) JSLL_MaxInt(void);
  71. extern JS_PUBLIC_API(JSInt64) JSLL_MinInt(void);
  72. extern JS_PUBLIC_API(JSInt64) JSLL_Zero(void);
  73. #endif
  74.  
  75. #define JSLL_MAXINT   JSLL_MaxInt()
  76. #define JSLL_MININT   JSLL_MinInt()
  77. #define JSLL_ZERO     JSLL_Zero()
  78.  
  79. #ifdef JS_HAVE_LONG_LONG
  80.  
  81. #if JS_BYTES_PER_LONG == 8
  82. #define JSLL_INIT(hi, lo)  ((hi ## L << 32) + lo ## L)
  83. #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
  84. #define JSLL_INIT(hi, lo)  ((hi ## i64 << 32) + lo ## i64)
  85. #else
  86. #define JSLL_INIT(hi, lo)  ((hi ## LL << 32) + lo ## LL)
  87. #endif
  88.  
  89. /***********************************************************************
  90. ** MACROS:      JSLL_*
  91. ** DESCRIPTION:
  92. **      The following macros define portable access to the 64 bit
  93. **      math facilities.
  94. **
  95. ***********************************************************************/
  96.  
  97. /***********************************************************************
  98. ** MACROS:      JSLL_<relational operators>
  99. **
  100. **  JSLL_IS_ZERO        Test for zero
  101. **  JSLL_EQ             Test for equality
  102. **  JSLL_NE             Test for inequality
  103. **  JSLL_GE_ZERO        Test for zero or positive
  104. **  JSLL_CMP            Compare two values
  105. ***********************************************************************/
  106. #define JSLL_IS_ZERO(a)       ((a) == 0)
  107. #define JSLL_EQ(a, b)         ((a) == (b))
  108. #define JSLL_NE(a, b)         ((a) != (b))
  109. #define JSLL_GE_ZERO(a)       ((a) >= 0)
  110. #define JSLL_CMP(a, op, b)    ((JSInt64)(a) op (JSInt64)(b))
  111. #define JSLL_UCMP(a, op, b)   ((JSUint64)(a) op (JSUint64)(b))
  112.  
  113. /***********************************************************************
  114. ** MACROS:      JSLL_<logical operators>
  115. **
  116. **  JSLL_AND            Logical and
  117. **  JSLL_OR             Logical or
  118. **  JSLL_XOR            Logical exclusion
  119. **  JSLL_OR2            A disgusting deviation
  120. **  JSLL_NOT            Negation (one's compliment)
  121. ***********************************************************************/
  122. #define JSLL_AND(r, a, b)        ((r) = (a) & (b))
  123. #define JSLL_OR(r, a, b)        ((r) = (a) | (b))
  124. #define JSLL_XOR(r, a, b)        ((r) = (a) ^ (b))
  125. #define JSLL_OR2(r, a)        ((r) = (r) | (a))
  126. #define JSLL_NOT(r, a)        ((r) = ~(a))
  127.  
  128. /***********************************************************************
  129. ** MACROS:      JSLL_<mathematical operators>
  130. **
  131. **  JSLL_NEG            Negation (two's compliment)
  132. **  JSLL_ADD            Summation (two's compliment)
  133. **  JSLL_SUB            Difference (two's compliment)
  134. ***********************************************************************/
  135. #define JSLL_NEG(r, a)        ((r) = -(a))
  136. #define JSLL_ADD(r, a, b)     ((r) = (a) + (b))
  137. #define JSLL_SUB(r, a, b)     ((r) = (a) - (b))
  138.  
  139. /***********************************************************************
  140. ** MACROS:      JSLL_<mathematical operators>
  141. **
  142. **  JSLL_MUL            Product (two's compliment)
  143. **  JSLL_DIV            Quotient (two's compliment)
  144. **  JSLL_MOD            Modulus (two's compliment)
  145. ***********************************************************************/
  146. #define JSLL_MUL(r, a, b)        ((r) = (a) * (b))
  147. #define JSLL_DIV(r, a, b)        ((r) = (a) / (b))
  148. #define JSLL_MOD(r, a, b)        ((r) = (a) % (b))
  149.  
  150. /***********************************************************************
  151. ** MACROS:      JSLL_<shifting operators>
  152. **
  153. **  JSLL_SHL            Shift left [0..64] bits
  154. **  JSLL_SHR            Shift right [0..64] bits with sign extension
  155. **  JSLL_USHR           Unsigned shift right [0..64] bits
  156. **  JSLL_ISHL           Signed shift left [0..64] bits
  157. ***********************************************************************/
  158. #define JSLL_SHL(r, a, b)     ((r) = (JSInt64)(a) << (b))
  159. #define JSLL_SHR(r, a, b)     ((r) = (JSInt64)(a) >> (b))
  160. #define JSLL_USHR(r, a, b)    ((r) = (JSUint64)(a) >> (b))
  161. #define JSLL_ISHL(r, a, b)    ((r) = (JSInt64)(a) << (b))
  162.  
  163. /***********************************************************************
  164. ** MACROS:      JSLL_<conversion operators>
  165. **
  166. **  JSLL_L2I            Convert to signed 32 bit
  167. **  JSLL_L2UI           Convert to unsigned 32 bit
  168. **  JSLL_L2F            Convert to floating point
  169. **  JSLL_L2D            Convert to floating point
  170. **  JSLL_I2L            Convert signed to 64 bit
  171. **  JSLL_UI2L           Convert unsigned to 64 bit
  172. **  JSLL_F2L            Convert float to 64 bit
  173. **  JSLL_D2L            Convert float to 64 bit
  174. ***********************************************************************/
  175. #define JSLL_L2I(i, l)        ((i) = (JSInt32)(l))
  176. #define JSLL_L2UI(ui, l)        ((ui) = (JSUint32)(l))
  177. #define JSLL_L2F(f, l)        ((f) = (JSFloat64)(l))
  178. #define JSLL_L2D(d, l)        ((d) = (JSFloat64)(l))
  179.  
  180. #define JSLL_I2L(l, i)        ((l) = (JSInt64)(i))
  181. #define JSLL_UI2L(l, ui)        ((l) = (JSInt64)(ui))
  182. #define JSLL_F2L(l, f)        ((l) = (JSInt64)(f))
  183. #define JSLL_D2L(l, d)        ((l) = (JSInt64)(d))
  184.  
  185. /***********************************************************************
  186. ** MACROS:      JSLL_UDIVMOD
  187. ** DESCRIPTION:
  188. **  Produce both a quotient and a remainder given an unsigned
  189. ** INPUTS:      JSUint64 a: The dividend of the operation
  190. **              JSUint64 b: The quotient of the operation
  191. ** OUTPUTS:     JSUint64 *qp: pointer to quotient
  192. **              JSUint64 *rp: pointer to remainder
  193. ***********************************************************************/
  194. #define JSLL_UDIVMOD(qp, rp, a, b) \
  195.     (*(qp) = ((JSUint64)(a) / (b)), \
  196.      *(rp) = ((JSUint64)(a) % (b)))
  197.  
  198. #else  /* !JS_HAVE_LONG_LONG */
  199.  
  200. #ifdef IS_LITTLE_ENDIAN
  201. #define JSLL_INIT(hi, lo) {JS_INT32(lo), JS_INT32(hi)}
  202. #else
  203. #define JSLL_INIT(hi, lo) {JS_INT32(hi), JS_INT32(lo)}
  204. #endif
  205.  
  206. #define JSLL_IS_ZERO(a)         (((a).hi == 0) && ((a).lo == 0))
  207. #define JSLL_EQ(a, b)           (((a).hi == (b).hi) && ((a).lo == (b).lo))
  208. #define JSLL_NE(a, b)           (((a).hi != (b).hi) || ((a).lo != (b).lo))
  209. #define JSLL_GE_ZERO(a)         (((a).hi >> 31) == 0)
  210.  
  211. #ifdef DEBUG
  212. #define JSLL_CMP(a, op, b)      (JS_ASSERT((#op)[1] != '='), JSLL_REAL_CMP(a, op, b))
  213. #define JSLL_UCMP(a, op, b)     (JS_ASSERT((#op)[1] != '='), JSLL_REAL_UCMP(a, op, b))
  214. #else
  215. #define JSLL_CMP(a, op, b)      JSLL_REAL_CMP(a, op, b)
  216. #define JSLL_UCMP(a, op, b)     JSLL_REAL_UCMP(a, op, b)
  217. #endif
  218.  
  219. #define JSLL_REAL_CMP(a,op,b)   (((JSInt32)(a).hi op (JSInt32)(b).hi) || \
  220.                                  (((a).hi == (b).hi) && ((a).lo op (b).lo)))
  221. #define JSLL_REAL_UCMP(a,op,b)  (((a).hi op (b).hi) || \
  222.                                  (((a).hi == (b).hi) && ((a).lo op (b).lo)))
  223.  
  224. #define JSLL_AND(r, a, b)       ((r).lo = (a).lo & (b).lo, \
  225.                                  (r).hi = (a).hi & (b).hi)
  226. #define JSLL_OR(r, a, b)        ((r).lo = (a).lo | (b).lo, \
  227.                                  (r).hi = (a).hi | (b).hi)
  228. #define JSLL_XOR(r, a, b)       ((r).lo = (a).lo ^ (b).lo, \
  229.                                  (r).hi = (a).hi ^ (b).hi)
  230. #define JSLL_OR2(r, a)          ((r).lo = (r).lo | (a).lo, \
  231.                                  (r).hi = (r).hi | (a).hi)
  232. #define JSLL_NOT(r, a)          ((r).lo = ~(a).lo, \
  233.                                  (r).hi = ~(a).hi)
  234.  
  235. #define JSLL_NEG(r, a)          ((r).lo = -(JSInt32)(a).lo, \
  236.                                  (r).hi = -(JSInt32)(a).hi - ((r).lo != 0))
  237. #define JSLL_ADD(r, a, b) { \
  238.     JSInt64 _a, _b; \
  239.     _a = a; _b = b; \
  240.     (r).lo = _a.lo + _b.lo; \
  241.     (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
  242. }
  243.  
  244. #define JSLL_SUB(r, a, b) { \
  245.     JSInt64 _a, _b; \
  246.     _a = a; _b = b; \
  247.     (r).lo = _a.lo - _b.lo; \
  248.     (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
  249. }
  250.  
  251. #define JSLL_MUL(r, a, b) { \
  252.     JSInt64 _a, _b; \
  253.     _a = a; _b = b; \
  254.     JSLL_MUL32(r, _a.lo, _b.lo); \
  255.     (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
  256. }
  257.  
  258. #define jslo16(a)        ((a) & JS_BITMASK(16))
  259. #define jshi16(a)        ((a) >> 16)
  260.  
  261. #define JSLL_MUL32(r, a, b) { \
  262.      JSUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
  263.      _a1 = jshi16(a), _a0 = jslo16(a); \
  264.      _b1 = jshi16(b), _b0 = jslo16(b); \
  265.      _y0 = _a0 * _b0; \
  266.      _y1 = _a0 * _b1; \
  267.      _y2 = _a1 * _b0; \
  268.      _y3 = _a1 * _b1; \
  269.      _y1 += jshi16(_y0);                         /* can't carry */ \
  270.      _y1 += _y2;                                /* might carry */ \
  271.      if (_y1 < _y2)    \
  272.         _y3 += (JSUint32)(JS_BIT(16));  /* propagate */ \
  273.      (r).lo = (jslo16(_y1) << 16) + jslo16(_y0); \
  274.      (r).hi = _y3 + jshi16(_y1); \
  275. }
  276.  
  277. #define JSLL_UDIVMOD(qp, rp, a, b)    jsll_udivmod(qp, rp, a, b)
  278.  
  279. extern JS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b);
  280.  
  281. #define JSLL_DIV(r, a, b) { \
  282.     JSInt64 _a, _b; \
  283.     JSUint32 _negative = (JSInt32)(a).hi < 0; \
  284.     if (_negative) { \
  285.     JSLL_NEG(_a, a); \
  286.     } else { \
  287.     _a = a; \
  288.     } \
  289.     if ((JSInt32)(b).hi < 0) { \
  290.     _negative ^= 1; \
  291.     JSLL_NEG(_b, b); \
  292.     } else { \
  293.     _b = b; \
  294.     } \
  295.     JSLL_UDIVMOD(&(r), 0, _a, _b); \
  296.     if (_negative) \
  297.     JSLL_NEG(r, r); \
  298. }
  299.  
  300. #define JSLL_MOD(r, a, b) { \
  301.     JSInt64 _a, _b; \
  302.     JSUint32 _negative = (JSInt32)(a).hi < 0; \
  303.     if (_negative) { \
  304.     JSLL_NEG(_a, a); \
  305.     } else { \
  306.     _a = a; \
  307.     } \
  308.     if ((JSInt32)(b).hi < 0) { \
  309.     JSLL_NEG(_b, b); \
  310.     } else { \
  311.     _b = b; \
  312.     } \
  313.     JSLL_UDIVMOD(0, &(r), _a, _b); \
  314.     if (_negative) \
  315.     JSLL_NEG(r, r); \
  316. }
  317.  
  318. #define JSLL_SHL(r, a, b) { \
  319.     if (b) { \
  320.     JSInt64 _a; \
  321.         _a = a; \
  322.         if ((b) < 32) { \
  323.         (r).lo = _a.lo << ((b) & 31); \
  324.         (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
  325.     } else { \
  326.         (r).lo = 0; \
  327.         (r).hi = _a.lo << ((b) & 31); \
  328.     } \
  329.     } else { \
  330.     (r) = (a); \
  331.     } \
  332. }
  333.  
  334. /* a is an JSInt32, b is JSInt32, r is JSInt64 */
  335. #define JSLL_ISHL(r, a, b) { \
  336.     if (b) { \
  337.     JSInt64 _a; \
  338.     _a.lo = (a); \
  339.     _a.hi = 0; \
  340.         if ((b) < 32) { \
  341.         (r).lo = (a) << ((b) & 31); \
  342.         (r).hi = ((a) >> (32 - (b))); \
  343.     } else { \
  344.         (r).lo = 0; \
  345.         (r).hi = (a) << ((b) & 31); \
  346.     } \
  347.     } else { \
  348.     (r).lo = (a); \
  349.     (r).hi = 0; \
  350.     } \
  351. }
  352.  
  353. #define JSLL_SHR(r, a, b) { \
  354.     if (b) { \
  355.     JSInt64 _a; \
  356.         _a = a; \
  357.     if ((b) < 32) { \
  358.         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
  359.         (r).hi = (JSInt32)_a.hi >> ((b) & 31); \
  360.     } else { \
  361.         (r).lo = (JSInt32)_a.hi >> ((b) & 31); \
  362.         (r).hi = (JSInt32)_a.hi >> 31; \
  363.     } \
  364.     } else { \
  365.     (r) = (a); \
  366.     } \
  367. }
  368.  
  369. #define JSLL_USHR(r, a, b) { \
  370.     if (b) { \
  371.     JSInt64 _a; \
  372.         _a = a; \
  373.     if ((b) < 32) { \
  374.         (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
  375.         (r).hi = _a.hi >> ((b) & 31); \
  376.     } else { \
  377.         (r).lo = _a.hi >> ((b) & 31); \
  378.         (r).hi = 0; \
  379.     } \
  380.     } else { \
  381.     (r) = (a); \
  382.     } \
  383. }
  384.  
  385. #define JSLL_L2I(i, l)        ((i) = (l).lo)
  386. #define JSLL_L2UI(ui, l)        ((ui) = (l).lo)
  387. #define JSLL_L2F(f, l)        { double _d; JSLL_L2D(_d, l); (f) = (JSFloat64)_d; }
  388.  
  389. #define JSLL_L2D(d, l) { \
  390.     int _negative; \
  391.     JSInt64 _absval; \
  392.  \
  393.     _negative = (l).hi >> 31; \
  394.     if (_negative) { \
  395.     JSLL_NEG(_absval, l); \
  396.     } else { \
  397.     _absval = l; \
  398.     } \
  399.     (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
  400.     if (_negative) \
  401.     (d) = -(d); \
  402. }
  403.  
  404. #define JSLL_I2L(l, i)        { JSInt32 _i = (i) >> 31; (l).lo = (i); (l).hi = _i; }
  405. #define JSLL_UI2L(l, ui)      ((l).lo = (ui), (l).hi = 0)
  406. #define JSLL_F2L(l, f)        { double _d = (double)f; JSLL_D2L(l, _d); }
  407.  
  408. #define JSLL_D2L(l, d) { \
  409.     int _negative; \
  410.     double _absval, _d_hi; \
  411.     JSInt64 _lo_d; \
  412.  \
  413.     _negative = ((d) < 0); \
  414.     _absval = _negative ? -(d) : (d); \
  415.  \
  416.     (l).hi = _absval / 4.294967296e9; \
  417.     (l).lo = 0; \
  418.     JSLL_L2D(_d_hi, l); \
  419.     _absval -= _d_hi; \
  420.     _lo_d.hi = 0; \
  421.     if (_absval < 0) { \
  422.     _lo_d.lo = -_absval; \
  423.     JSLL_SUB(l, l, _lo_d); \
  424.     } else { \
  425.     _lo_d.lo = _absval; \
  426.     JSLL_ADD(l, l, _lo_d); \
  427.     } \
  428.  \
  429.     if (_negative) \
  430.     JSLL_NEG(l, l); \
  431. }
  432.  
  433. #endif /* !JS_HAVE_LONG_LONG */
  434.  
  435. JS_END_EXTERN_C
  436.  
  437. #endif /* jslong_h___ */
  438.